<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 登录账号设置 -->
          <el-tab-pane label="登录账号设置">
            <!-- 放置表单 -->
            <el-form
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
              :model="userInfo"
              :rules="rules"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input style="width: 300px" v-model="userInfo.username" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input
                  style="width: 300px"
                  type="password"
                  v-model="userInfo.password"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleUpdate">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 个人详情  -->
          <el-tab-pane label="个人详情">
            <el-tooltip class="tooltip-box" content="打印基本个人信息">
              <router-link
                :to="`/employees/print/${userInfo.id}?type=personal`"
              >
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <!-- 内容 -->
            <user-info />
          </el-tab-pane>
          <!-- 岗位信息 -->
          <el-tab-pane label="岗位信息">
            <!-- 内容 -->
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserInfoAPI } from "@/api/user";
import { putEmployeeAPI } from "@/api/employees";
import UserInfo from "./components/user-info.vue";
import JobInfo from "./components/job-info.vue";
export default {
  name: "EmployeesDetail",
  components: {
    UserInfo,
    JobInfo,
  },
  data() {
    return {
      // 用户信息
      userInfo: {
        username: "",
        password: "",
      },
      // 校验规则
      rules: {
        username: [
          {
            required: true,
            message: "姓名不能为空",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          {
            min: 6,
            max: 9,
            message: "密码必须是6-9位",
            trigger: "blur",
          },
        ],
      },
    };
  },
  async created() {
    try {
      await this.getUserInfo();
    } catch (e) {}
  },
  methods: {
    // 根据 ID 获取员工详情
    async getUserInfo() {
      // 1. 发请获取详情请求
      const resp = await getUserInfoAPI(this.$route.params.id);
      // 2. 保存详情对象数据
      this.userInfo = resp.data;
    },
    // 修改员工信息
    async handleUpdate() {
      // 1. 发请更新请求
      await putEmployeeAPI(this.userInfo);
      // 2. 提示语
      this.$message.success("员工信息更新成功");
      // 3. 跳回员工页
      this.$router.push("/employees");
    },
  },
};
</script>

<style lang="scss">
.employees-detail-container {
  .el-tab-pane {
    padding-top: 10px;
  }
  .tooltip-box {
    position: absolute;
    right: 30px;
    top: 10px;
    z-index: 999;
  }
}
</style>
